home *** CD-ROM | disk | FTP | other *** search
- class Vector2D
- {
- var _x = 0;
- var _y = 0;
- function Vector2D(x, y)
- {
- this._x = x;
- this._y = y;
- }
- function SetToZero()
- {
- this._x = this._y = 0;
- }
- function SetToRandomNormal()
- {
- this.SetToAngleNormal(_root.random.GetRandom() * 3.141592653589793 * 2);
- }
- function Set(v)
- {
- this._x = v._x;
- this._y = v._y;
- }
- function GetCopy()
- {
- return new Vector2D(this._x,this._y);
- }
- function GetStringForm()
- {
- return "( " + this._x + ", " + this._y + " )";
- }
- function IsEqual(v)
- {
- return v._x == this._x && v._y == this._y;
- }
- function IsZero(epsilon)
- {
- return this._x <= epsilon && this._y <= epsilon;
- }
- function Magnitude()
- {
- return Math.sqrt(this.MagnitudeSquared());
- }
- function MagnitudeSafe()
- {
- var _loc2_ = this.MagnitudeSquared();
- if(_loc2_ > 0)
- {
- return Math.sqrt(_loc2_);
- }
- return 0;
- }
- function MagnitudeSquared()
- {
- return this._x * this._x + this._y * this._y;
- }
- function GetDistance(v)
- {
- return this.GetSubtract(v).MagnitudeSafe();
- }
- function GetDistanceSquared(v)
- {
- return this.GetSubtract(v).MagnitudeSquared();
- }
- function Rotate(angleRadians)
- {
- var _loc3_ = Math.cos(angleRadians);
- var _loc2_ = Math.sin(angleRadians);
- var _loc5_ = _loc3_ * this._x - _loc2_ * this._y;
- var _loc4_ = _loc2_ * this._x + _loc3_ * this._y;
- this._x = _loc5_;
- this._y = _loc4_;
- }
- function SetToAngleNormal(angleRadians)
- {
- this._x = Math.cos(angleRadians);
- this._y = Math.sin(angleRadians);
- }
- function Normalize()
- {
- var _loc2_ = this.Magnitude();
- if(_loc2_ > 0)
- {
- this._x /= _loc2_;
- this._y /= _loc2_;
- }
- }
- function GetNormal()
- {
- var _loc2_ = new Vector2D(this._x,this._y);
- _loc2_.Normalize();
- return _loc2_;
- }
- function Invert()
- {
- this._x = - this._x;
- this._y = - this._y;
- }
- function Transpose()
- {
- var _loc2_ = this._x;
- this._x = this._y;
- this._y = _loc2_;
- }
- function GetInverse()
- {
- return new Vector2D(- this._x,- this._y);
- }
- function GetAngle()
- {
- return Math.atan2(this._y,this._x);
- }
- function GetAngleDegrees()
- {
- return Math.atan2(this._y,this._x) / 3.141592653589793 * 180;
- }
- function AddScalar(scalar)
- {
- this._x += scalar;
- this._y += scalar;
- }
- function SubtractScalar(scalar)
- {
- this._x -= scalar;
- this._y -= scalar;
- }
- function MultiplyScalar(scalar)
- {
- this._x *= scalar;
- this._y *= scalar;
- }
- function DivideScalar(scalar)
- {
- this._x /= scalar;
- this._y /= scalar;
- }
- function Add(v)
- {
- this._x += v._x;
- this._y += v._y;
- }
- function Subtract(v)
- {
- this._x -= v._x;
- this._y -= v._y;
- }
- function Multiply(v)
- {
- this._x *= v._x;
- this._y *= v._y;
- }
- function Divide(v)
- {
- this._x /= v._x;
- this._y /= v._y;
- }
- function DotProduct(v)
- {
- return this._x * v._x + this._y * v._y;
- }
- function GetAddScalar(scalar)
- {
- var _loc2_ = new Vector2D(this._x,this._y);
- _loc2_.AddScalar(scalar);
- return _loc2_;
- }
- function GetSubtractScalar(scalar)
- {
- var _loc2_ = new Vector2D(this._x,this._y);
- _loc2_.SubtractScalar(scalar);
- return _loc2_;
- }
- function GetMultiplyScalar(scalar)
- {
- var _loc2_ = new Vector2D(this._x,this._y);
- _loc2_.MultiplyScalar(scalar);
- return _loc2_;
- }
- function GetDivideScalar(scalar)
- {
- var _loc2_ = new Vector2D(this._x,this._y);
- _loc2_.DivideScalar(scalar);
- return _loc2_;
- }
- function GetAdd(v)
- {
- var _loc2_ = new Vector2D(this._x + v._x,this._y + v._y);
- return _loc2_;
- }
- function GetSubtract(v)
- {
- var _loc2_ = new Vector2D(this._x - v._x,this._y - v._y);
- return _loc2_;
- }
- function GetMultiply(v)
- {
- var _loc2_ = new Vector2D(this._x * v._x,this._y * v._y);
- return _loc2_;
- }
- function SnapToMajorAxis()
- {
- if(Math.abs(this._x) > Math.abs(this._y))
- {
- this._y = 0;
- }
- else
- {
- this._x = 0;
- }
- this.Normalize();
- }
- function SnapToMinorAxis()
- {
- if(Math.abs(this._x) < Math.abs(this._y))
- {
- this._y = 0;
- }
- else
- {
- this._x = 0;
- }
- this.Normalize();
- }
- }
-